﻿using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;

namespace UFrame
{
    /// <summary>
    /// 配置各种项目需要的信息, 路径
    /// </summary>
    public class UFrameConfig
    {
        [RuntimeInitializeOnLoadMethod]
        [DidReloadScripts]
        [InitializeOnLoadMethod]
        public static void LoadAssetConfig()
        {
            //Debug.Log("加载配置....");
            TextAsset t = Resources.Load<TextAsset>("UFrameAssetEditor");
            if (t == null)
            {
                return;
            }

            var lines = t.text.Split('\n');

            string path = string.Empty;

            path = StringTool.LineByKeyPair("UIDefineClass", lines);
            if (!string.IsNullOrEmpty(path))
            {
                PLUIMenu.classPath = path;
            }

            path = StringTool.LineByKeyPair("UIDefinePrefabs", lines);
            if (!string.IsNullOrEmpty(path))
            {
                PLUIMenu.uiDirPath = path;
            }

            //小游戏存档代码生成
            path = StringTool.LineByKeyPair("PlayerPrefsPath", lines);
            if (!string.IsNullOrEmpty(path))
            {
                PlayerPrefsCodeGenerate.path = path;
            }
            path = StringTool.LineByKeyPair("PlayerPrefsOutPath", lines);
            if (!string.IsNullOrEmpty(path))
            {
                PlayerPrefsCodeGenerate.outPath = path;
            }
            
            path = StringTool.LineByKeyPair("CopyAndroidStudioResPath", lines);
            if (!string.IsNullOrEmpty(path))
            {
                BuildGameEditor.copyAndroidStudioResPath = path;
            }
            
            
            path = StringTool.LineByKeyPair("PlayerPrefsClass", lines);
            if (!string.IsNullOrEmpty(path))
            {
                var classList = PlayerPrefsCheckWindow.classSaveName;
                if (path.Contains(","))
                {
                    var splitAry = path.Split(',');
                    foreach (var item in splitAry)
                    {
                        if (!classList.Contains(item))
                        {
                            PlayerPrefsCheckWindow.classSaveName.Add(item);
                        }
                    }
                }
                else
                {
                    if (!classList.Contains(path))
                        PlayerPrefsCheckWindow.classSaveName.Add(path);
                }
            }
            
            //sprite批量转换的路径
            path = StringTool.LineByKeyPair("SpriteConvertPath", lines);
            if (!string.IsNullOrEmpty(path))
            {
                var dic = SpriteAssetPostprocessor.pathList;
                if (path.Contains(","))
                {
                    var splitAry = path.Split(',');
                    foreach (var item in splitAry)
                    {
                        if (!dic.ContainsKey(item))
                        {
                            dic.Add(item, item);
                        }
                    }
                }
                else
                {
                    if (!dic.ContainsKey(path))
                        dic.Add(path, path);
                }
            }
        }
    }
}